package com.example.hzq.food.fragment;

import android.content.Context;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.SearchView;
import android.widget.Toast;

import com.example.hzq.food.R;
import com.example.hzq.food.adapter.ShopFoodadapter;
import com.example.hzq.food.bean.FoodBean;
import com.example.hzq.food.model.Model;

import java.util.List;

import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;

/**
 * Created by hzq on 2017/6/28.
 */

public class Fragment2 extends Fragment {
    private View view=null;
    private Context context;
    private RecyclerView recyclerView;
    private RecyclerView.LayoutManager manager;
    private ShopFoodadapter foodadapter;
    private List<FoodBean> list;
    private SearchView searchView;
    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        view = inflater.inflate(R.layout.fragment2, container, false);
        context = getActivity();
        recyclerView=(RecyclerView)view.findViewById(R.id.searchfood);
        manager=new LinearLayoutManager(context);
        recyclerView.setLayoutManager(manager);
        recyclerView.setHasFixedSize(true);
        searchView=(SearchView)view.findViewById(R.id.searchview);
        searchView.setIconifiedByDefault(false);
        searchView.setOnQueryTextListener(new SearchView.OnQueryTextListener() {
            @Override
            public boolean onQueryTextSubmit(String query) {
                Model model=new Model();
                Call<List<FoodBean>> call= model.getfoodbysearch(query);
                call.enqueue(new Callback<List<FoodBean>>() {
                    @Override
                    public void onResponse(Call<List<FoodBean>> call, Response<List<FoodBean>> response) {
                        if(response.isSuccessful()&&response.body()!=null)
                        {
                            list=response.body();
                            foodadapter.setList(list);
                        }
                        else
                        {
                            Toast.makeText(context,"获取资源失败",Toast.LENGTH_SHORT).show();
                        }
                    }

                    @Override
                    public void onFailure(Call<List<FoodBean>> call, Throwable t) {
                        Toast.makeText(context,"请检查网络连接",Toast.LENGTH_SHORT).show();
                    }
                });
                return true;
            }
            @Override
            public boolean onQueryTextChange(String newText) {
                if (!TextUtils.isEmpty(newText)){
                    Model model=new Model();
                    Call<List<FoodBean>> call= model.getfoodbysearch(newText);
                    call.enqueue(new Callback<List<FoodBean>>() {
                        @Override
                        public void onResponse(Call<List<FoodBean>> call, Response<List<FoodBean>> response) {
                            if(response.isSuccessful()&&response.body()!=null)
                            {
                                list=response.body();
                                foodadapter.setList(list);
                            }
                            else
                            {
                                Toast.makeText(context,"获取资源失败",Toast.LENGTH_SHORT).show();
                            }
                        }

                        @Override
                        public void onFailure(Call<List<FoodBean>> call, Throwable t) {
                            Toast.makeText(context,"请检查网络连接",Toast.LENGTH_SHORT).show();
                        }
                    });
                }else{
                    list=null;
                }
                return true;
            }
        });
        foodadapter=new ShopFoodadapter(context);
        foodadapter.setList(list);
        recyclerView.setAdapter(foodadapter);
        return view;
    }
}
